package org.happyinsure.structure;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class Row {
	
	private static List<String> header = Arrays.asList("Date", "Name", "Address", "Premium", "Type", "Note");
	public static String separator = ",\t";
	private final List<Object> row = Arrays.asList(null, null, null, null, null, null);
	
	public static int getColumnCount() {
		return header.size();
	}
	
	public static int getDateColumnIndex() {
		return header.indexOf("Date");
	}
	
	public static int getNameColumnIndex() {
		return header.indexOf("Name");
	}
	
	public static int getAddressColumnIndex() {
		return header.indexOf("Address");
	}
	
	public static int getPremiumColumnIndex() {
		return header.indexOf("Premium");
	}
	
	public static int getTypeColumnIndex() {
		return header.indexOf("Type");
	}
	
	public static int getNoteColumnIndex() {
		return header.indexOf("Note");
	}
	
	public static String getColumnName(final int index) {
		return header.get(index);
	}

	public Row (){}
	
	public Object getCell(final int index) {
		if (index == getDateColumnIndex())
			return new SimpleDateFormat("yyyy-MMM-dd").format((Date) row.get(index));
		return row.get(index);
	}
	
	public void setCell(final int index, final String data) throws ParseException {
		switch (index) {
		case 0:
			row.set(index, new SimpleDateFormat("yyyy-MMM-dd").parse(data));
			break;
		case 3:
			row.set(index, Double.parseDouble(data));
			break;
		case 4:
			row.set(index, Type.valueOf(data));
			break;
		default:
			row.set(index, data);
		}
	}
	
	@Override
	public String toString() {
		String result = "";
		for (Object cell : row) {
			result += cell + separator;
		}
		return result;
	}

	public static enum Type {
		TradingCombine,
		CommercialProperty,
		ResidentialLet,
		OwnerOccupied,
		Others
	}

}
